// 封装axios请求模块
import axios from "axios";
import store from "@/store";

const request = axios.create({
  //配置公共请求头
  baseURL: "http://localhost:8000/",
});

// 配置请求拦截器 给请求统一添加token
request.interceptors.request.use(
  function(config) {
    const user = store.state.user;
    if (user) {
      config.headers.Authorization = `Bearer ${user.token}`;
    }
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// 将请求头暴露出去
export default request;
